*******************************
* IRCA and Redistricting
* Navid Sabet and Noam Yuchtman
* Table E1: Hispanic Members of Congress
*******************************
	
	//Load Data
	use hispanic_members_of_congress_list.dta, clear
		
	*******************************************************
	* List of all hispanic representatives in our data
	*******************************************************
	tab congress
	bysort bioname: egen first_congress = min(congress)
	bysort bioname: egen last_congress = max(congress)
	gen number_congress_served = (last_congress - first_congress)+1
	gen first_congress_sort = first_congress
	gen last_congress_sort = last_congress
	
	tostring first_congress, replace
	tostring last_congress, replace
	
	gen service_range = first_congress + "-" + last_congress
	replace service_range = first_congress if number_congress_served == 1
	
	bysort bioname: gen counter = _n
	sort StateFIPS first_congress_sort
	order bioname state district nominate_dim1 number_congress_served service_range
	sort StateFIPS first_congress_sort
	br bioname StateFIPS state district nominate_dim1 service_range number_congress_served party_code if counter==1 & hisp_representative==1 & last_congress_sort>=98
	sum nominate_dim1 number_congress_served service_range if counter==1 & hisp_representative==1

	
	*******************************************************
	* Identify switchers: non-Hispanic to Hispanic seat
	* Identify non-switchers: Hispanic to Hispanics
	*******************************************************
	//Hispanic replace non-hispanic:
	bysort StateFIPS district_code (congress): gen hisp_replacement = 1 if  hisp_representative==1 & hisp_representative[_n-1]==0
	//DW nominate of the predecessor. 
	bysort StateFIPS district_code (congress): replace hisp_replacement = 1 if  hisp_representative==0 & hisp_representative[_n+1]==1
	
	//Hispanic replace a hispanic
	bysort StateFIPS district_code (congress): gen hisp_hisp = 1 if  hisp_representative==1 & hisp_representative[_n-1]==1
	bysort StateFIPS district_code (congress): replace hisp_hisp = 1 if  hisp_representative==1 & hisp_representative[_n+1]==1
	//DW nominate of previous representative
	bysort StateFIPS district_code (congress): gen dw_previous = nominate_dim1[_n-1]
	
	//Hisp status of predecessor. Replace this with "." if he was the incumbent
	gen hisp_previous = 999
	bysort StateFIPS district_code (congress): replace hisp_previous = hisp_representative[_n-1]
	bysort StateFIPS district_code (congress): replace hisp_previous = . if bioname == bioname[_n-1]
	
	sort StateFIPS district_code congress 
	order state_name district_code congress bioname party_code nominate_dim1 hisp_representative
	gen party = "DEM" if party_code==100
	replace party = "REP" if party_code==200
	order state_name district_code congress bioname party  nominate_dim1 hisp_representative
	
	drop counter 
	bysort StateFIPS district_code bioname (congress): gen counter = _n
	
	bysort StateFIPS district_code bioname (congress): egen min_counter = min(counter) if congress >102
	
	//Hispanic status and DW nominate of predecessor (not previous rep)
	sort StateFIPS district_code congress
	br bioname nominate_dim1 dw_previous hisp_previous counter congress district_code StateFIPS if StateFIPS == 4 & district_code==2
	bysort StateFIPS district_code congress: gen dw_predecessor_help = dw_previous if counter == 1
	bysort StateFIPS district_code congress: gen hisp_predecessor_help = hisp_previous if counter == 1
	
	bysort StateFIPS district_code bioname (congress): egen dw_predecessor = min(dw_predecessor_help)
	bysort StateFIPS district_code bioname (congress): egen hisp_predecessor = min(hisp_predecessor_help)
	
	drop hisp_predecessor_help dw_predecessor_help
	
	gen hisp_predecessor_string = "Yes" if hisp_predecessor == 1
	replace hisp_predecessor_string = "No" if hisp_predecessor == 0
	replace hisp_predecessor_string = "." if hisp_predecessor == .
	
	//There are two cases when the DW nominate of the predecessor is ".". This is because the district was first established in the 1990 redistricting cycle (i.e, January 3 1993)! These are Florida's 21st district and Texas 28th.
	//See here: https://en.wikipedia.org/wiki/Florida%27s_21st_congressional_district and here: https://en.wikipedia.org/wiki/Texas%27s_28th_congressional_district
	
	//Final list of Hispanics and the members they replaced: Table E.1
	br state_name district_code bioname party  nominate_dim1 service_range hisp_predecessor_string dw_predecessor if hisp_representative == 1 & (hisp_replacement==1 | hisp_hisp==1) & congress>102 & min_counter==counter

	rename state_name State
	rename district_code District
	rename bioname Representative
	rename party Party
	rename nominate_dim1 DW_Nominate
	rename service_range Congress_Served
	rename dw_predecessor DW_Predecessor
	rename hisp_predecessor_string Pred_Hispanic
